package com.twelve.all.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SP工具类
 */
public class SPUtil {

    private static final String PREF_NAME = "com.hangwei.paperless";

    private static SharedPreferences getPref() {
        return Tools.getAppContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static void remove(String key) {
        getPref().edit().remove(key).apply();
    }

    public static void setInt(final String key, final int value) {
        getPref().edit().putInt(key, value).apply();
    }

    public static int getInt(final String key) {
        return getPref().getInt(key, 0);
    }

    public static int getInt(final String key, int defValue) {
        return getPref().getInt(key, defValue);
    }

    public static String getString(final String key) {
        return getPref().getString(key, "");
    }

    public static String getString(String key, String defaultValue) {
        return getPref().getString(key, defaultValue);
    }

    public static void setString(final String key, final String value) {
        getPref().edit().putString(key, value).apply();
    }

    public static void setBoolean(final String key, final boolean value) {
        getPref().edit().putBoolean(key, value).apply();
    }

    public static boolean getBoolean(final String key, boolean def) {
        return getPref().getBoolean(key, def);
    }

    public static void setLong(final String key, final long value) {
        getPref().edit().putLong(key, value).apply();
    }

    public static long getLong(final String key, long def) {
        return getPref().getLong(key, def);
    }

    public static boolean isExist(String key) {
        return getPref().contains(key);
    }

    /**
     * 清空所有共享变量
     */
    public static void removeAll() {
        getPref().edit().clear().apply();
    }
}